package com.cloudfun.campusshare.configuration.jpa.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 王溪源
 * @ClassName: ServletUtils
 * @Description: TODO
 **/
public class ServletUtils {
    public static Map<String, Object> getParameterStartingWith(HttpServletRequest request, String prefix) {
        Enumeration<String> parameterNames = request.getParameterNames ();
        if (prefix == null) {
            prefix = "";
        }
        Map<String, Object> map = new HashMap<> ();
        while (parameterNames != null && parameterNames.hasMoreElements ()) {
            String paramName = parameterNames.nextElement ();
            if (StringUtils.isBlank (prefix) || paramName.startsWith (prefix)) {
                String unPrefixed = paramName.substring (prefix.length ());
                String[] values = request.getParameterValues (paramName);
                if (values != null && values.length > 1) {
                    map.put (unPrefixed, values);
                } else if (values != null && values.length == 1) {
                    map.put (unPrefixed, values[0]);
                }
            }
        }
        return null;
    }
}
